package com.levy.contronller.javaWeb.threadConcurrence.countDownLatch;

import java.util.concurrent.CountDownLatch;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by levy on 2019/4/8.
 */

public class Worker implements Runnable {

    private CountDownLatch downLatch;
    private String name;

    public Worker(CountDownLatch downLatch, String name) {
        this.downLatch = downLatch;
        this.name = name;
    }

    @Override
    public void run() {
        this.doWork();
        System.out.println(this.downLatch.toString());
        try {
            TimeUnit.SECONDS.sleep(new Random().nextInt(10));
        } catch (InterruptedException ie) {
        }


        this.downLatch.countDown();
        long count = this.downLatch.getCount();
        if (count == 0) {
            System.out.println("所有活干完了！");
        }else{
            System.out.println(this.name + "活干完了！还需要等待"+ count +"人干完活！");

        }

    }

    private void doWork() {
        System.out.println(this.name + "正在干活!");
    }

}
